package com.cloudkinto.service.consumer.platformProduct;

import com.alibaba.fastjson.JSONObject;
import com.cloudkinto.common.RabbitQueueConstant;
import com.cloudkinto.service.consumer.vo.order.UpdateInventoryLessReq;
import com.cloudkinto.service.platform.PlatformProductService;
import com.cloudkinto.service.platform.vo.product.PlatProductUpdateConsumerReq;
import com.cloudkinto.service.platform.vo.rakuten.PlatformProductSyncReq;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class PlatformProductConsumer {
    private final PlatformProductService platformProductService;

    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformProduct_InventoryLess))
    public void updateInventoryLess(String msg) {
        try {
            UpdateInventoryLessReq req = JSONObject.parseObject(msg, UpdateInventoryLessReq.class);
            platformProductService.updateInventoryLess(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * 库存回传
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformProduct_Rakuten_UpdateByParentSKu))
    public void updateRakutenByParentSku(String msg) {
        try {
            PlatformProductSyncReq req = JSONObject.parseObject(msg, PlatformProductSyncReq.class);
            platformProductService.save(req.getProductDoList(), req.getPlatformCode(), req.isDelete(), req.getStoreId(), req.getCompanyId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * 批量更新
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformProduct_ImportUpdate))
    public void importBatchUpdate(String msg) {
        try {
            PlatProductUpdateConsumerReq req = JSONObject.parseObject(msg, PlatProductUpdateConsumerReq.class);
            platformProductService.importUpdate(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
